import { computed, defineComponent, onMounted, onUnmounted, reactive, Ref, ref, toRaw, nextTick } from 'vue';
import type { Resubmit, ReqParam, ResData, ResDataSearch, OpButtonKey } from '@/domain/dataBoard/types/longclass/resubmit';
import { getResubmit, getSelection } from '@/domain/dataBoard/api/longClass/resubmit';
import { usePageList, apiType } from '@/domain/common/hooks/pageList';
import type { Column, OpGroupCallBack, ResSearch } from '@/domain/common/types/page';
import { useUserInfoStore } from '@/domain/authority/hooks/login';
import { ChildId, ErrorChildId, PrimaryID } from '@/domain/dataBoard/types/common';
import { Overview } from '@/domain/common/types/common';

export const useResubmitData = (childId: ChildId) => {
  const searchRef: Ref<ResSearch> = ref([]);
  const api: apiType<Resubmit, ReqParam> = {
    list: getResubmit,
    search: searchRef.value
  };
  const userStore = useUserInfoStore();

  const {
    IDString,
    rowlist,
    columnlist,
    paging,
    searchParam,
    searchList,
    setSizeChange,
    loadTable,
    getSelectRowsCallBack,
    reloadList,
    getResData,
    tableLoading
  } = usePageList<PrimaryID, Resubmit, ReqParam>(api, 'id');
  const overview: Ref<Overview> = computed(() => getResData()?.overview!);
  //页面操作回掉
  const opGroupCallBack: OpGroupCallBack<Resubmit, OpButtonKey> = async (key, node) => {};

  const dataSource = computed(() => rowlist.value.map((node) => Object.assign(node.row, { opGroup: node.op_group })));

  onMounted(async () => {
    const res = await getSelection();
    searchList.value = res.data;
    if (childId !== ErrorChildId.DefaultChildId) {
      (searchParam as ReqParam).child_id = childId;
    }
  });

  return {
    IDString,
    dataSource,
    rowlist,
    paging,
    searchParam,
    columnlist,
    searchList,
    overview,
    setSizeChange,
    loadTable,
    getSelectRowsCallBack,
    opGroupCallBack,
    reloadList,
    tableLoading
  };
};
